package bg.cinema_system.DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Date;

import bg.cinema_system.global.ConnectionJDBC;

public class ReservationDAO {

	public static List<Integer> getReservedSeats(int projection_id) throws SQLException  {
		
		Connection dbConnection = ConnectionJDBC.getConnection();

		if (dbConnection == null) {
			throw new SQLException("Class ReservationDAO: Can't get database connection");
		}

		String sqlStatement = "SELECT seats.seat_number FROM cinema.seats " 
		+ "JOIN cinema.reservations ON seats.fk_reservation_id = reservations.reservation_id "
		+ "WHERE reservations.fk_projection_id = ?";

		PreparedStatement ps = null;
		ResultSet result = null;
		List<Integer> reserved_seats = new ArrayList<Integer>();
		try {
			ps = dbConnection.prepareStatement(sqlStatement);
			ps.setInt(1, projection_id);
			
			// Get customer data from database
			result = ps.executeQuery();
			
			while (result.next()) {
				reserved_seats.add(result.getInt("seat_number"));
			}
		} catch (SQLException e) {
			throw new SQLException(e);
		} finally {
			try {
				// Close all connections
				ps.close();
				result.close();
				dbConnection.close();
			} catch (SQLException e) {
				throw new SQLException(e);
			}
		}

		return reserved_seats;
	}
	
	public static List<Integer> getPaidSeats(int projection_id) throws SQLException {
		
		Connection dbConnection = ConnectionJDBC.getConnection();

		if (dbConnection == null) {
			throw new SQLException("Class ReservationDAO: Can't get database connection");
		}

		String sqlStatement = "SELECT seats.seat_number FROM cinema.seats " 
		+ "JOIN cinema.tickets ON seats.fk_ticket_id = tickets.ticket_id "
		+ "WHERE tickets.fk_projection_id = ?";

		PreparedStatement ps = null;
		ResultSet result = null;
		List<Integer> reserved_seats = new ArrayList<Integer>();
		try {
			ps = dbConnection.prepareStatement(sqlStatement);
			ps.setInt(1, projection_id);
			
			// Get customer data from database
			result = ps.executeQuery();
			
			while (result.next()) {
				reserved_seats.add(result.getInt("seat_number"));
			}
		} catch (SQLException e) {
			throw new SQLException(e);
		} finally {
			try {
				// Close all connections
				ps.close();
				result.close();
				dbConnection.close();
			} catch (SQLException e) {
				throw new SQLException(e);
			}
		}

		return reserved_seats;
	}
	
	public static boolean makeReservation(int user_id, int projection_id, Date expires_at) throws SQLException  {
		
		Connection dbConnection = ConnectionJDBC.getConnection();

		if (dbConnection == null) {
			throw new SQLException("Class ReservationDAO: Can't get database connection");
		}

		String sqlStatement = "INSERT INTO cinema.reservations " 
		+ " (expires_at, fk_user_id, fk_projection_id) "
		+ " VALUES (?, ?, ?)";

		PreparedStatement ps = null;
		int result = 0;
		try {
			ps = dbConnection.prepareStatement(sqlStatement);
			ps.setTimestamp(1, new java.sql.Timestamp(expires_at.getTime()));
			ps.setInt(2, user_id);
			ps.setInt(3, projection_id);
			
			result = ps.executeUpdate();
		} catch (SQLException e) {
			throw new SQLException(e);
		} finally {
			try {
				// Close all connections
				ps.close();
				dbConnection.close();
			} catch (SQLException e) {
				throw new SQLException(e);
			}
		}

		if (result != 0) {
			return true;
		} else {
			return false;
		}
	}
	
	public static int getReservationId(int projection_id, int user_id) throws SQLException {
		
		Connection dbConnection = ConnectionJDBC.getConnection();

		if (dbConnection == null) {
			throw new SQLException("Class ReservationDAO: Can't get database connection");
		}

		String sqlStatement = "SELECT reservations.reservation_id FROM cinema.reservations " 
		+ "WHERE reservations.fk_projection_id = ? "
		+ "AND reservations.fk_user_id = ?";

		PreparedStatement ps = null;
		ResultSet result = null;
		int reservetion_id = -1;
		try {
			ps = dbConnection.prepareStatement(sqlStatement);
			ps.setInt(1, projection_id);
			ps.setInt(2, user_id);
			
			// Get customer data from database
			result = ps.executeQuery();
			if (result.next()) {
				reservetion_id = result.getInt("reservation_id");
			}
		} catch (SQLException e) {
			throw new SQLException(e);
		} finally {
			try {
				// Close all connections
				ps.close();
				dbConnection.close();
			} catch (SQLException e) {
				throw new SQLException(e);
			}
		}

		return reservetion_id;
	}
}
